package core
import (
  /* 标准库 */
  "net/http"
  "fmt"
  "encoding/json"
  "io"

  /* 项目内部包 */
  "dog.com"
  "dog.com/redis"
  "dog.com/lru"
)

/******************** homeService 常量 **********************/

/******************** homeService 常量 **********************/

/******************** homeService 结构体 ********************/

// job 即继承 Ijob 接口
type RedisLruJob struct {
    RequestContent string                        /* post请求json */
    redisLruData   redisLruStruct
}

/* redis服务请求 */
type redisLruStruct struct {
  RedisKey       string  `json:"key"`
  RedisData      string  `json:"data"`
  Time           string  `json:"time"`           /* 执行时间     */
}

/* 接收redisService服务的报文用该结构体解析 */
type RedisLruRequestStruct struct {
  Title           string         `json:"name"`
  ServiceType     string         `json:"serviceType"`
  ServiceCode     string         `json:"serviceCode"`
  RequestContent  string         `json:"data"`
}

// RedisLruJob 必须实现 Ijob 接口的UnpackRequest
func (rJob *RedisLruJob) UnpackRequest() {
    fmt.Println("RedisLruJob UnpackRequest! = ", rJob.RequestContent)
    if error := json.Unmarshal([]byte(rJob.RequestContent), &rJob.redisLruData); nil != error {
        fmt.Println(" Unmarshal error " , error )
    }
    fmt.Println("Do redisKey = ", rJob.redisLruData.RedisKey )
}

func RedisLruServ (w  http.ResponseWriter, r *http.Request) {

    var redisLruRequest RedisLruRequestStruct
    fmt.Println("RedisLruServ start!")
    err := json.NewDecoder(io.LimitReader(r.Body, MaxPostLength)).Decode(&redisLruRequest)
    if err !=  nil {
        w.Header().Set("Content-Type", "application/json; charset=UTF-8")
        w.WriteHeader(http.StatusBadRequest)
        return
    }

    serviceType := redisLruRequest.ServiceType

    //获取全局服务Map
    JobMap := dog.WorkMapFactory()
    if JobMap == nil {
        fmt.Println("JobMap is nil")
        return
    }

    // 从JobMap中取出 具体work
    if _, exists := JobMap[serviceType]; !exists {
        fmt.Println("in map Ijob is nil")
        return
    }

    work := JobMap[serviceType]

    //把请求内容存放到work结构体中
    if redisLruWork , ok := work.(*RedisLruJob) ; ok {
        redisLruWork.RequestContent = redisLruRequest.RequestContent
        work = redisLruWork
    } else {
        fmt.Println(" Ijob interface converte failed!")
        return
    }

    work1 := RedisLruJob { RequestContent : redisLruRequest.RequestContent}

    dog.JobQueue <- work1

    w.WriteHeader(http.StatusOK)
}

// RedisLruJob 必须实现 Ijob 接口的UnpackRequest
func (rJob *RedisLruJob) Do() {
    fmt.Println("RedisLruJob Do!")

    l := lru.FactoryLru()
    fmt.Println("Do redisKey = ", rJob.redisLruData.RedisKey )

    //
    var outChan chan lru.LruOutChan = make (chan lru.LruOutChan )
    var inLruChan lru.LruInChan  = lru.LruChan {ChanOut : outChan , Key : rJob.redisLruData.RedisKey }

    //
    l.inChan <- inLruChan
    // 接收处理返回
    outLruChan :<- outChan

    //if delRedisKey, ok := l.Process(rJob.redisLruData.RedisKey); ok {
    delRedisKey := outLruChan.DelKey
    if outLruChan.Flag {
        if "" != delRedisKey {
            // 删除 redis 内存
            if _, ok := dogredis.Redis.DelStringType(delRedisKey); ok {
                fmt.Println("del redis key [" , delRedisKey , "] faild!")
            }
        } else {
            // 不需要删除key ，因为 Lru 队列没有存满 不需要删除 redis 内容
        }
    }
}

// ReidsLruJob 必须实现 Ijob 接口的UnpackRequest
func (rJob *RedisLruJob) PackResponse() {
    fmt.Println("RedisLruJob PackResponse!")
}
